import { defineStore } from 'pinia';

interface User {
  id: number;
  // 其他属性
}

export const useUserStore = defineStore('user', {
  state: () => ({
    user: null as User | null,
  }),
  getters: {
    getUser: (state) => state.user,
    isAuthenticated: (state) => !!state.user,
  },
  actions: {
    setUser(newUser: User | null) {
      this.user = newUser;
      sessionStorage.setItem('user', JSON.stringify(newUser)); // 存储用户信息到 sessionStorage
    },
    logout() {
      this.user = null;
      sessionStorage.removeItem('user'); // 从 sessionStorage 中移除用户信息
    },
    initUser() {
      const userData = sessionStorage.getItem('user');
      if (userData) {
        this.user = JSON.parse(userData); // 从 sessionStorage 中恢复用户信息
      }
    },
  },
});